/****************************** (C) COPYRIGHT 2016 Rsestudio ****************************
* File Name          : 
* Author             : RSES_Liyang
* Version            : V1.0.0
* Date               : 00-May-20
* Description        : 
*****************************************************************************************/

#ifndef __MAIN__H__
#define __MAIN__H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ----------------------------------------------------------------------------*/
#include "r_math.h"
/* Exported define ---------------------------------------------------------------------*/

//#define bat_charge_cnt (*(uint32_t*)(0x20007814))

#define ERROR_FLAG_MEMORY_DET               (1<<2)
#define ERROR_FLAG_BAT_MANAGER              (1<<3)

//--------------------------------------------------------------------
#define EFLOG_LVL_ERROR         1
#define EFLOG_LVL_ALARM         2
#define EFLOG_LVL_INFO          3
#define EFLOG_LVL_DEBUG         5
    


    
//Powermanager






//--------------------------------------------------------------------
#define MEAS_COND_START_MANUAL				0
#define MEAS_COND_START_TEMPERATURE	 		1
#define MEAS_COND_START_TIME				2
#define MEAS_COND_START_TandT				3
#define MEAS_COND_START_TorT				4
#define MEAS_COND_START_TEtoTI				5
#define MEAS_COND_START_COMAND				10

#define MEAS_COND_STOP_MANUAL				0
#define MEAS_COND_STOP_TEMPERATURE			1
#define MEAS_COND_STOP_TIME					2
#define MEAS_COND_STOP_TandT				3
#define MEAS_COND_STOP_TorT					4
#define MEAS_COND_STOP_TEtoTI				5

#define MEAS_FILE_STOP_MANULA				0
#define MEAS_FILE_STOP_COND					1
#define MEAS_FILE_STOP_COMM					2
#define MEAS_FILE_STOP_HTEMP				3
#define MEAS_FILE_STOP_LBAT					4
#define MEAS_FILE_STOP_MEM					5
#define MEAS_FILE_STOP_RECV					6
#define MEAS_FILE_STOP_NONE					26

#define COMMAND_CTRL_NONE					0
#define COMMAND_CTRL_START					1
#define COMMAND_CTRL_COND_START				2
#define COMMAND_CTRL_DIRECT_START			3	
#define COMMAND_CTRL_STOP					4

#define COMMAND_CTRL_STS_NONE				0
#define COMMAND_CTRL_STS_IDLE				1
#define COMMAND_CTRL_STS_COND				2
#define COMMAND_CTRL_STS_MEAS				3
#define COMMAND_CTRL_STS_PAUSE				4

#define COMMAND_CTRL_STS_PREPARE            5

//-------------------------------------------------------
#define MEAS_SELECT_TEMPERATURE             (1<<0)
#define MEAS_SELECT_VIBRATION               (1<<1)
#define MEAS_SELECT_OXYGEN                  (1<<2)
//-------------------------------------------------------

#define COMM_CMD_MAX                		64

#define CMD_NONE							0x00
#define CMD_SET_RestoreFactory				0x02
#define CMD_SET_Device_Reset				0x03
#define CMD_SET_Update              		0x08
#define CMD_GET_DeviceInfo					0x09
#define CMD_SET_DeviceInfo					0x10 
#define CMD_GET_DeviceID					0x11

#define CMD_GET_FlashCapacity				0x12

#define CMD_LOG_Opera                       0x13

#define CMD_GET_DeviceConf					0x1A
#define CMD_SET_DeviceConf					0x1B 
#define CMD_DeviceMem_opera					0x1C
#define CMD_GET_ProcessData					0x20
//21 22 23 BLE reserve //

#define CMD_GET_raw_data					0x2F
#define CMD_GET_GroupFileList				0x30
#define CMD_GET_GroupFileStream				0x31
#define CMD_GET_FileStreamFast				0x32
#define CMD_OP_Calib						0x33
#define CMD_SET_DateTime					0x34
#define CMD_GET_MeasParams					0x35
#define CMD_SET_MeasParams					0x36
#define CMD_CTL_MeasControl					0x37
#define CMD_GET_MeasMemory					0x38
#define CMD_GET_MeasMemory_usb				0x39
#define CMD_DEL_GroupFile    				0x3a
#define CMD_GET_GroupBigFileStream    				0x3b
#define CMD_GET_WIFI_UPGRADE  				0x3c
#define CMD_GET_WIFI_FIRMWARE				0x3d


//-----------------------------------------------

#define TEMPERATURE_MAX_CHANNEL             25
#define MAX_DPOINT_CALIB					4

#define LOG_TAG_POWER                       10
#define LOG_TAG_HARD                        11
#define LOG_TAG_UI_PAGE_IN                  12
#define LOG_TAG_CMD                         13



// LOG_TAG PWOER
#define LOG_POWER_ON                        1
#define LOG_POWER_OFF_TIMEOUT               2
#define LOG_POWER_OFF_MANUAL                3
#define LOG_POWER_OFF_LOWBAT                4


// LOG_TAG HARD
#define LOG_HARD_PM_INIT_FAULT              1
#define LOG_HARD_PM_REINIT                  2
#define LOG_HARD_MEAS_READ_COLD_ERR         10
#define LOG_HARD_INNER_TEMP_FAULT           11
#define LOG_HARD_OUTER_TEMP_FAULT           12

//-----------------------------------------------------------
#define TD_PRI_NORMAL                       16		//10
#define TD_PRI_LOW                          15		//12
#define TD_PRI_HIGH                         18		//8
#define TD_PRI_HARD                         20		//7
/* Exported macro ----------------------------------------------------------------------*/
//extern uint32_t SYS_SESET ;
//extern uint32_t SYS_PM_VALUE ;
//extern uint32_t SYS_PM_INIT_FLAG ;

//extern int32_t PM_BAT_CAP_MAX;					
//extern int32_t PM_BAT_CAP_MIN;							
//extern int32_t PM_BAT_CAP_FULL_SCALE;					
//extern int32_t PM_BAR_CHARGE_INC;	
/* Exported types ----------------------------------------------------------------------*/
//#pragma pack (4)
typedef struct
{
	int16_t Status;
	int16_t Rssi;
	uint16_t peripheralMTU;
	uint16_t connHandle;           // Connection handle of current connection
	uint16_t connInterval;
	uint16_t connSlaveLatency;
	uint16_t connTimeout;
} peripheralConnItem_t;



typedef struct _DATE
{
	unsigned char year;
	unsigned char Month;
	unsigned char Day;
	unsigned char Hour;
	unsigned char Minute;
	unsigned char Second;
	unsigned int UTC_Date;
}DATE,*PDATE;

typedef struct _MEAS_PARAMS
{	
    unsigned int  	SamplingSpan;
    unsigned int 	Start_Cond;
	unsigned int	Start_Params_1;
	unsigned int	Start_Params_2;
	unsigned int	Start_Params_3;
	unsigned int	Start_Params_4;
	
	unsigned int 	Stop_Cond;
	unsigned int	Stop_Params_1;
	unsigned int	Stop_Params_2;
	unsigned int	Stop_Params_3;
	unsigned int	Stop_Params_4;

}MEAS_PARAMS,*PMEAS_PARAMS;


typedef struct _SYS_INFO
{ 
	unsigned char       BatChargeStatus;
	signed char       	BatLevelStatus;  
	unsigned char       VbusOnline;
	unsigned char       HostConnect;
	unsigned short      BatTempetature;
	unsigned short      BatVoltage;
	DATE           		Date;
	unsigned char 		BoardConnect;
	
	unsigned int 		v_data_buf[8];
	signed   char       Group;
	
	unsigned char 		test_mode;

	unsigned short 		StartStatus;
	unsigned short 		CondStatus;
	unsigned short 		CondTimer;

	unsigned short 		MeasFileID;
	unsigned short      Error_id;
    unsigned char       Error_buf[16];
      
}SYS_STATUS,*PSYS_STATUS;

typedef struct _Command_Ctrl
{
	unsigned char 		Status;
	unsigned char 		Meas_ctrl;
	
	unsigned char		cond_stage;
	unsigned char       Temp_ch;
	
	unsigned short 		Temp_value;
	unsigned short 		Time_value;
	
	unsigned int 		Recorder_cnt;
    
    unsigned short      b_meas_select;

}COMMAND_CTRL;



typedef struct _sys_config
{
	unsigned char dev_main_type;
	unsigned char dev_sub_type;
	unsigned char dev_ext_type;
	unsigned char check_id;	
	unsigned char version_minor;
	unsigned char version_revison;
	unsigned char version_label;	
	unsigned char version_Major;
	
	unsigned char mfg_year;
	unsigned char mfg_month;
	unsigned char mfg_day;	
	unsigned char Total_channel;
	unsigned char dummy_1;
	unsigned char dummy_2;
	unsigned char serial_id[36];
	//---- 50Byte 
	
	//0
	unsigned char calib_year;
	unsigned char calib_month;
	unsigned char calib_day;	
	unsigned char calib_next_year;
	unsigned char calib_next_month;
	unsigned char calib_next_day;
	unsigned char warranty_year;
	unsigned char warranty_month;
	unsigned char 	warranty_day;
    
	unsigned char 	dummy_3;
	signed short 	over_temp_stop;
	signed short	start_max_temp;	
	signed short	start_max_tc_temp;
	signed short	Bat_capacity;
	
	unsigned char 	dev_name[32];
	//------48_byte------
}SYS_CONFIG;


typedef struct _temperature_data
{
    signed short 		ColdTemper;
	signed short 		temperature[TEMPERATURE_MAX_CHANNEL];
    
    signed char         ThermcoupleConnectStatus[TEMPERATURE_MAX_CHANNEL];
    signed char         total_connect;
    signed char         last_inster;
    signed char         new_inster_flag;
    signed char         dummy_1;
        
    signed char 		MaxTempChannel;
	signed char			MinTempChannel;
    signed char         dummy_2;
    signed char         dummy_3;
    
	signed short 		MaxTemperature;
	signed short 		MinTemperature;
    
    dpoint_calib_t      calib[TEMPERATURE_MAX_CHANNEL];
    dpoint_calib_t      cold_calib[2];
    
}temperature_data;

typedef struct _oxygen_data
{
	int 		heat_vh;
	int 		heat_vl;
	int 		heat_is;
	int 		sensor_out;	    
	int 		oxygen_value;	
    
	mdpoint_calib_t	mdp_calib;
}oxygen_data;

typedef struct _pressure_data
{
    int 		        pressure_data;
	int 		        pressure_data_raw;
    dpoint_calib_t      calib[1];
}pressure_data;

typedef struct _speed_data
{
    short               s1_value;
    short               s2_value;
	
	short               s1_raw;
    short               s2_raw;
	
	short               s1_offset;
    short               s2_offset;
	
}speed_data;

typedef struct _width_data
{
    unsigned char offset_select;
    unsigned char Res_1;
    unsigned char Res_2;
    unsigned char Res_3;
    
    int         raw;
    int         relative;
    int         offset;
    int         set_width;
}width_data;

typedef struct _vibrate_data
{
    short x_raw;
    short y_raw;
    short z_raw;
    
    short vib_x;
    short vib_y;
    short vib_z;
    
    short pitch_raw;
    short roll_raw;
    
    short pitch;
    short roll;
    
    average_filter_t avg_vib_x;
    average_filter_t avg_vib_y;
    average_filter_t avg_vib_z;
    
    average_filter_t avg_vib_result_x;
    average_filter_t avg_vib_result_y;
    average_filter_t avg_vib_result_z;
    
    dpoint_calib_t      calib[5];
       
}vibrate_data;


typedef struct _iProfile
{
	peripheralConnItem_t  	Ble;
	SYS_STATUS       		sys_status;
    SYS_CONFIG		  		conf;
    COMMAND_CTRL	  		cmd_ctrl;
	MEAS_PARAMS        		meas_params;
    MEAS_PARAMS             meas_params_cur;
    temperature_data        temperature;
    oxygen_data			    oxygen;
    vibrate_data            vibrate;
    pressure_data           pressure;
    speed_data              speed;
    width_data              r_width;
}iProfile_t;    

//#pragma pack ()
extern uint8_t v_data_table[];
extern iProfile_t iProfile;

int iProfile_main_start(void);
void meas_params_init(MEAS_PARAMS *pmeas_params);
void meas_params_copy(MEAS_PARAMS *psrc,MEAS_PARAMS *pdst);

#ifdef __cplusplus
}
#endif

#endif
/********************************(C)COPYRIGHT 2016 RSEStudio ****************************/
/*****************************************END OF FILE************************************/
